package com.os.ch3.aware;

import org.apache.commons.io.IOUtils;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.BeanNameAware;
import org.springframework.context.ResourceLoaderAware;
import org.springframework.core.io.Resource;
import org.springframework.core.io.ResourceLoader;
import org.springframework.stereotype.Service;

/**
 * @Author Matt
 * @Date 2022-01-11
 * @Desc: springAware演示类
 * @Version V1.0
 */
@Service
public class AwareService implements BeanNameAware, ResourceLoaderAware {  //实现接口，获得bean名称和资源加载的服务

    private String beanName;
    private ResourceLoader loader;

    public void setBeanName(String name) throws BeansException {
        this.beanName = name;
    }

    public void setResourceLoader(ResourceLoader resourceLoader) {
        this.loader = resourceLoader;
    }

    public void outputResult(){
        System.out.println("bean的名称为：" + beanName);

        Resource resource = loader.getResource("classpath:com/os/ch3/aware/test.txt");

        try {
            System.out.println("resourceLoader加载的文件为：" + IOUtils.toString(resource.getInputStream()));
        }catch (Exception e){
            e.printStackTrace();
        }

    }



}
